import { Tool, ToolColor } from '../../../Tool';

/**
 * 距离测量
 */
export class DistanceTool extends Tool {
	constructor(viewer, options = {}, ...args) {
		super(arguments);
		this.viewer = viewer;
		this.options = options;
		this.float = options?.float ?? 2;
		this.tempValue = 0;

		this.handler = new Cesium.MeasureHandler(this.viewer, Cesium.MeasureMode.Distance, 0);
		this.handler.lineColor = Cesium.Color.fromCssColorString(ToolColor.functionalColor);
		this.handler.lineWidth = 1;

		this.clickHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
		if (this.viewer.distanceDataSource) {
			this.dataSource = this.viewer.distanceDataSource;
		} else {
			this.dataSource = this.viewer.distanceDataSource = new Cesium.CustomDataSource('fe-distance');
			this.viewer.dataSources.add(this.dataSource);
		}
		viewer.scene.layers.layerQueue.forEach((item) => (item.selectEnabled = false));
		[...arguments].forEach((item) => {
			if (typeof item === 'function') {
				this.callback = item;
			}
		});
		this.callbackValue = {
			total: null,
			current: null,
			tool: this
		};
	}

	start() {
		let distanceNow = '';
		let lastEntity = null;

		this.clickHandler.setInputAction((e) => {
			const position = this.viewer.scene.pickPosition(e.position);
			if (!distanceNow) return;

			const distance = (parseFloat(distanceNow) - this.tempValue).toFixed(this.float);
			const distanceStr =
				distance > 1000 ? (distance / 1000).toFixed(this.float) + 'km' : distance + 'm';

			this.callbackValue.total = distanceNow;
			this.callbackValue.current = distanceStr;

			lastEntity = this.dataSource.entities.add({
				position: position,
				label: {
					text: '距离: ' + distanceStr,
					font: '14px sans-serif',
					showBackground: true,
					backgroundColor: Cesium.Color.fromCssColorString(
						ToolColor.toRgba(ToolColor.backgroundColor, 0.8)
					),
					fillColor: Cesium.Color.fromCssColorString(ToolColor.color),
					disableDepthTestDistance: Number.POSITIVE_INFINITY,
					verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
					pixelOffset: new Cesium.Cartesian2(72, 0)
				}
			});
			this.tempValue = parseFloat(distanceNow);

			this.callback && this.callback(this.callbackValue);
		}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

		this.handler.measureEvt.addEventListener((result) => {
			const distance = Number(result.distance).toFixed(this.float);
			const distanceStr =
				distance > 1000 ? (distance / 1000).toFixed(this.float) + 'km' : distance + 'm';
			distanceNow = distanceStr;

			const measureEntity = this.handler.disLabel;
			measureEntity.text = '总距离: ' + distanceStr;
			measureEntity.font = '14px sans-serif';
			measureEntity.showBackground = true;
			measureEntity.backgroundColor = Cesium.Color.fromCssColorString(
				ToolColor.toRgba(ToolColor.backgroundColor, 0.8)
			);
			measureEntity.fillColor = Cesium.Color.fromCssColorString(ToolColor.color);
			measureEntity.style = Cesium.LabelStyle.FILL;
			measureEntity.disableDepthTestDistance = Number.POSITIVE_INFINITY;
		});

		this.handler.activeEvt.addEventListener((isActive) => {
			if (isActive === true) {
				this.viewer.enableCursorStyle = true;
				this.viewer._element.style.cursor = 'Crosshair';
			} else {
				this.clickHandler.destroy();
				this.clickHandler = null;
				distanceNow = '';

				lastEntity && this.dataSource.entities.remove(lastEntity);
				lastEntity = null;
				this.viewer.enableCursorStyle = false;
				this.viewer._element.style.cursor = 'drag';
			}
		});

		this.handler.activate();
	}

	cancel() {
		this.dataSource.entities.removeAll();
		// this.viewer.dataSources.remove(this.dataSource, true)
		this.clickHandler?.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
		this.handler.clear();
		this.handler.deactivate();
		this.viewer.scene.layers.layerQueue.forEach((item) => (item.selectEnabled = true));
	}
}
DistanceTool.toolId = 'DistanceTool';
